layui.use(['form','layer','laydate','table','laytpl'],function() {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;


    //文章类别列表
    var tableIns = table.render({
        elem: '#newsList',
        url: '/personal_blog/blogTypes',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 20,
        limits: [10, 15, 20, 25],
        id: "newsListTable",
        cols: [[
            {field: 'id', title: 'ID', width: 60, align: "center",unresize:true},
            {field: 'typeName', title: '类别名称', width: 400,unresize:true},
            {field: 'orderNo', title: '排序编号',width: 100,align: 'center',unresize:true},
            {title: '操作', width: 200, templet: '#newsListBar', fixed: "right", align: "left"}
        ]]
    })

    $(".addType_btn").click(function(){
        addType();
    })




    function addType(data) {
        var index = layui.layer.open({
            title : "添加类别",
            type : 2,
            content : "/personal_blog/background/typeAdd",
            area: ["300px","240px"],
            success:function(layero, index){
                var body = $($(".layui-layer-iframe",parent.document).find("iframe")[0].contentWindow.document.body);
                body = $(body.find("iframe")[0].contentWindow.document.body);

                if(data){
                    body.find("#createTime").val(data.createTime);
                    body.find("#typeId").val(data.id);
                    body.find(".typeName").val(data.typeName);
                    body.find(".orderNo").val(data.orderNo);
                    form.render();
                }

            }


        })
        // layui.layer.full(index);
        // //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        // $(window).on("resize",function(){
        //     layui.layer.full(index);
        // })


    }



    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addType(data);
        } else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此类别？',{icon:3, title:'提示信息'},function(index){
                var url = "/personal_blog/blogTypes/"+data.id;
                axios.delete(url).then(function (response) {
                    tableIns.reload();
                    layer.close(index);
                })


                // $.get("删除文章接口",{
                //     newsId : data.newsId  //将需要删除的newsId作为参数传入
                // },function(data){

                // })
            });
        } else if(layEvent === 'look'){ //预览
            layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问")
        }
    });

    //添加类别表单提交
    form.on("submit(addType)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});

        var bean = {
            typeName:$(".typeName").val(),
            orderNo:$(".orderNo").val(),
        }
        var msg = "";
        if($("#typeId").val()){
            bean.id=$("#typeId").val();
            bean.createTime=$("#createTime").val();
            axios.put("/personal_blog/blogTypes",bean).then(function(response){
                msg = "更新成功！"
            })
        }else{
            axios.post("/personal_blog/blogTypes",bean).then(function(response){
                msg = "新增成功！"
            })
        }

        setTimeout(function(){
            top.layer.close(index);
            top.layer.msg(msg);
            layer.closeAll("iframe");
            window.parent.location.reload();//刷新父页面
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);
        },500);
        return false;
    })


})